package com.yanqu.road.server.manger.activity.dafuweng;

import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.activity.dafuweng.DFWEvent;
import com.yanqu.road.entity.activity.dafuweng.DFWUserData;
import com.yanqu.road.entity.enums.activity.dafuweng.eDaFuWengEventType;
import com.yanqu.road.logic.config.GameConfig;

import java.util.ArrayList;

/**
 * 参数不匹配时修复
 */
public class AutoFixLogic {

    public static void fixParam(DFWUserData userData) {
        if (userData == null) {
            return;
        }
        DFWConfig config = DaFuWengMgr.getDfwConfig();
        if (config == null) {
            return;
        }
        DFWEvent event = config.getDfwEventMap().get(userData.getEventId());

        // 参数设置经过时间太短
        if (System.currentTimeMillis() - userData.getLastSetEventTime() < GameConfig.DA_FU_WENG_EVENT_PARAM_CHECK_TIME) {
            return;
        }

        // 不是密道事件有密道数据
        if (!userData.getUserSecretRoadList().isEmpty() && (event == null || event.getEventType() != eDaFuWengEventType.Secret)) {
            userData.setUserSecretRoadList(new ArrayList<>());
            DaFuWengMgr.getLogger().error("DaFuWengMgr AutoFixLogic : fix no in secret event but has secretRoadList! userId={}, eventId={}, expandParam={}, positionIndex={}",
                    userData.getUserId(), userData.getEventId(), userData.getExpandParam(), userData.getPositionIndex());
            return;
        }

        if (event == null) {
            return;
        }

        // 后面的事件已完成不用处理
        if (userData.isComplete()) {
            return;
        }

        if (event.getEventType() == eDaFuWengEventType.Boss) {
            // 【Boss事件】
            try {
                String expandParam = userData.getExpandParam();
                String[] split = expandParam.split(";");
                if (split.length != 2 ||
                        Integer.valueOf(split[0]) <= 0 ||
                        Integer.valueOf(split[1]) < 0) {
                    throw new Exception();
                }
            } catch (Exception e) {
                DaFuWengMgr.getLogger().error("DaFuWeng auto fix boss event! userId={}, eventId={}, expandParam={}",
                        userData.getUserId(), userData.getEventId(), userData.getExpandParam());
                userData.setComplete(true);
                userData.setExpandParam("");
            }
        } else if (event.getEventType() == eDaFuWengEventType.ZaoYao) {
            // 【造谣事件】
            if (userData.getZaoYaoOptionalList().isEmpty()) {
                DaFuWengMgr.getLogger().error("DaFuWeng auto fix zao yao event! userId={}, no optional union list!",
                        userData.getUserId());
                userData.setComplete(true);
                userData.setExpandParam("");
            }
        } else if (event.getEventType() == eDaFuWengEventType.Secret) {
            // 【密道事件】
            // 没密道数据跳过
            if (userData.getUserSecretRoadList().isEmpty()) {
                DaFuWengMgr.getLogger().error("DaFuWeng auto fix secret road event! userId={}, expandParam={}, positionIndex={}, eventId={}, no userSecretRoadList!",
                        userData.getUserId(), userData.getExpandParam(), userData.getPositionIndex(), userData.getEventId());
                userData.setComplete(true);
                userData.setExpandParam("");
            } else {
                // 没下标参数重置为-1
                try {
                    Integer index = Integer.valueOf(userData.getExpandParam());
                    if (index < -1) {
                        throw new Exception();
                    }
                } catch (Exception e) {
                    DaFuWengMgr.getLogger().error("DaFuWeng auto fix secret road event! userId={}, expandParam={}, no int expandParam!",
                            userData.getUserId(), userData.getExpandParam());
                    userData.setExpandParam("-1");
                }
            }
        }

    }

}
